﻿(function ($) {
    $.fn.resizeable = function (option) {
        option = $.extend({
    }, option);
    return this.each(function () {
        var view = $(this);
        var settings = $.extend({}, option);
        var helper = new resizeableHelper(view, settings);
        view.helper("resizeable", helper);
        helper.init();
    });
};
var resizeableHelper = function (jo, settings) {
    this.jo = jo;
    this.settings = settings;
    var _this = this;
    this.direction = null;
    this.enresize = true;
    this.mhelper = null;
    this.mcover = null; //防止iframe丢失mouseover
    this.init = function () {
        this.jo.addClass("resizeable");
        var position = this.jo.css("position");
        if (position != "absolute" || position != "relative") {
            this.jo.css("position", "absolute");
        }
        var html = "<div class='rtl'></div>\
                    <div class='rt'></div>\
                    <div class='rtr'></div>\
                    <div class='rl'></div>\
                    <div class='rr'></div>\
                    <div class='rbl'></div>\
                    <div class='rb'></div>\
                    <div class='rbr'></div>";
        this.jo.append($(html));
        this.jo.children(".rt,.rb,.rl,.rr,.rtl,.rtr,.rbl,.rbr").on('mousedown', this.start);

    };
    this.enable = function () {
        this.enresize = true;
        this.jo.children(".rt,.rb,.rl,.rr,.rtl,.rtr,.rbl,.rbr").show();
    };
    this.disable = function () {
        this.enresize = false;
        this.jo.children(".rt,.rb,.rl,.rr,.rtl,.rtr,.rbl,.rbr").hide();
    };
    this.start = function (e) {
        if (!_this.enresize) return;
        document.onselectstart = function (e) { return false };
        _this.direction = $(e.target || e.srcElement).attr("class");
        var o = _this.jo;
        var position = {
            width: o.width(),
            height: o.height(),
            x: e.pageX || e.screenX,
            y: e.pageY || e.screenY
        };
        o.data("resizeposition", position);
        var resizehelper = $("<div class=\"resizehelper\"></div>");
        //resizehelper.appendTo(_this.jo);
        resizehelper.appendTo($("body"));
        resizehelper.css({ left: o.offset().left, top: o.offset().top });
        resizehelper.width(o.width());
        resizehelper.height(o.height());
        var zindex = _this.jo.css("z-index");
        if (!zindex) {
            zindex = 1;
        } else {
            zindex = zindex * 1 + 1;
        }
        resizehelper.css({ zIndex: zindex });
        _this.mhelper = resizehelper;

        var cover = $('<div style="position:absolute;left:0;top:0;z-index:9999"></div>');
        cover.width(o.width());
        cover.height(o.height());
        _this.mcover = cover;
        cover.appendTo(_this.jo);

        $(document).on("mousemove", _this.drag).on("mouseup", _this.stop);
        return _this.preventEvent(e);
    };
    this.drag = function (e) {
        if (!e) var e = window.event;
        if (!_this.enresize) return;
        var currposition = {
            x: e.pageX || e.screenX,
            y: e.pageY || e.screenY
        };
        var o = _this.jo;
        var oposition = o.data("resizeposition");
        var dertx = 0;
        if (_this.direction == "rl" ||
            _this.direction == "rr" ||
            _this.direction == "rtl" ||
            _this.direction == "rtr" ||
            _this.direction == "rbl" ||
            _this.direction == "rbr") {
            dertx = currposition.x - oposition.x;
        }
        var derty = 0;
        if (_this.direction == "rt" ||
            _this.direction == "rtl" ||
            _this.direction == "rtr" ||
            _this.direction == "rb" ||
            _this.direction == "rbl" ||
            _this.direction == "rbr") {
            derty = currposition.y - oposition.y;
        }
        _this.mhelper.width(oposition.width + dertx);
        _this.mhelper.height(oposition.height + derty);
        return _this.preventEvent(e);
    };
    this.stop = function (e) {
        if (!_this.enresize) return;
        document.onselectstart = function (e) { return true };
        _this.jo.width(_this.mhelper.width());
        _this.jo.height(_this.mhelper.height());
        _this.mhelper.remove();
        _this.jo.layout();
        $(document).off('mousemove', _this.drag).off('mouseup', _this.stop);
        _this.mcover.remove();

        return _this.preventEvent(e);
    };
    this.preventEvent = function (e) {
        if (e.stopPropagation) e.stopPropagation();
        if (e.preventDefault) e.preventDefault();
        return false;
    }
}
})(jQuery);